package cn.com.xinjinfu.pdoulcProduct.activity.FixedProduct;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.DoWorkActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseActivity;
import cn.com.xinjinfu.pdoulcProduct.activity.base.BaseWebActivity;
import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.entity.ActivitMoneyBean;
import cn.com.xinjinfu.pdoulcProduct.entity.ActivitMoneyBean.ExpGoods.Page;
import cn.com.xinjinfu.pdoulcProduct.entity.LandlordIncomeBean;
import cn.com.xinjinfu.pdoulcProduct.entity.LandlordIncomeBean.AppListBrokerIncome;
import cn.com.xinjinfu.pdoulcProduct.entity.User;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;
import cn.com.xinjinfu.pdoulcProduct.manager.TitleManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.manager.UIManager;
import cn.com.xinjinfu.pdoulcProduct.utils.SharedPreferencesUtils;
import cn.com.xinjinfu.pdoulcProduct.utils.TextViewUtil;
import cn.com.xinjinfu.pdoulcProduct.xlistview.XListView;

/**
 * 地主收益
 * Created by android-adt on 2016/1/12.
 */
public class LandlordIncomeAct extends BaseActivity implements XListView.IXListViewListener {
    private ListView mListView;
    private RequestQueue requen;
    private MyAdapter mAdapter;
    private LandlordIncomeBean landlordIncomeBean;
    private TextView hine_income, tv_landlord_money, tv_person_num, tv_exper_income,tv_month_income
            ,tv_actite_info;
    private List<AppListBrokerIncome> page;
    private LinearLayout show_income;
    private Dialog dg;
    private String url = DataHandler.WEBURL + "/front/wa/bannerLink?seri=14";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_landlord_income);
        //初始化头布局
        TitleManager.showTitleWhite(this, "地主收益", "");
        show_income = (LinearLayout) findViewById(R.id.show_income);
        hine_income = (TextView) findViewById(R.id.hine_income);
        tv_landlord_money = (TextView) findViewById(R.id.tv_landlord_money);
        tv_person_num = (TextView) findViewById(R.id.tv_person_num);
        tv_exper_income = (TextView) findViewById(R.id.tv_exper_income);
        tv_month_income = (TextView) findViewById(R.id.tv_month_income);
        tv_actite_info = (TextView) findViewById(R.id.tv_actite_info);
        tv_actite_info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("url", url);
                bundle.putString("title", "兜地主规则");
                Intent intent = new Intent(LandlordIncomeAct.this, BaseWebActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        mListView = (ListView) findViewById(R.id.listView_activite);
//        mListView.setPullLoadEnable(false);// 是否显示下拉刷新和上啦加载
//        mListView.setPullRefreshEnable(false);
        mAdapter = new MyAdapter(this);
        mListView.setAdapter(mAdapter);
//        mListView.setXListViewListener(this);
        requen = Volley.newRequestQueue(this);
        loginRequest();
    }

    public void upData() {
        if (landlordIncomeBean != null) {
            double investAmount = landlordIncomeBean.getInvestAmount();
            if (investAmount>=1000){
                show_income.setVisibility(View.VISIBLE);
                hine_income.setVisibility(View.GONE);
            }else {
                show_income.setVisibility(View.GONE);
                hine_income.setVisibility(View.VISIBLE);
            }
            tv_landlord_money.setText(TextViewUtil.getSumLength(landlordIncomeBean.getInvestRebate()));
            tv_person_num.setText("昨日在投好友 "+ landlordIncomeBean.getRecommendBrokerNumber()+" 人");
            tv_exper_income.setText(TextViewUtil.getStringTwoLength(landlordIncomeBean.getTotalBrokerIncome()));
            tv_month_income.setText(TextViewUtil.getSumLength(landlordIncomeBean.getBrokerMonthIncome()));
            page = landlordIncomeBean.getAppListBrokerIncome();
            mAdapter.setList(page);
            mAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 网络请求
     */
    private void loginRequest() {
        dg = UIManager.getLoadingDialog(this);
        dg.setCanceledOnTouchOutside(false);
        dg.show();
        User user = AccountManager.getInstance().getUser();
        if (user != null) {
            final Map<String, String> parameters = DataHandler.getParameters("2040");
            parameters.put("id", user.getCurrUser().getSign());
            JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters), null, succeedRespon, error);
            requen.add(request);
        }

    }

    private Response.ErrorListener error = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            dg.dismiss();
            DataHandler.errorHandler(LandlordIncomeAct.this, error);
        }
    };
    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            dg.dismiss();
            landlordIncomeBean = JSON.parseObject(jobj.toString(), LandlordIncomeBean.class);
            upData();
        }
    };

    @Override
    public void onRefresh() {
    }

    @Override
    public void onLoadMore() {
    }

    private class MyAdapter extends BaseAdapter {

        private LayoutInflater mInflater;
        private List<AppListBrokerIncome> list;

        public void setList(List<AppListBrokerIncome> list) {
            this.list = list;
        }

//        禁止点击
        @Override
        public boolean isEnabled(int position) {
            return false;
        }


        public MyAdapter(Context context) {
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_landlord_income, null);
                holder = new ViewHolder();
                holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
                holder.tv_income = (TextView) convertView.findViewById(R.id.tv_income);
                convertView.setTag(holder);//绑定ViewHolder对象
            } else {
                holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
            }
            AppListBrokerIncome page = list.get(position);
            if (page != null) {
                holder.tv_income.setText(TextViewUtil.getSumLength(page.getIncome()));
                holder.tv_time.setText(page.getMonth());
            }
            return convertView;
        }
    }

    public final class ViewHolder {
        public TextView tv_income;
        public TextView tv_time;
    }
}